<template>
    <div class="app-container content-wrapper">
        <div class="filter-container">
            <el-form :model="listQuery" :inline="true" ref="filterForm" style="display: inline-block">
                <!--
                <el-form-item prop="carFullNum" class="filter-item">
                    <el-input v-model="listQuery.carFullNum" placeholder="车牌号" style="width: 200px;"></el-input>
                </el-form-item>

                <el-button class="filter-item" type="primary" icon="search" @click="handleFilter">搜索</el-button>
                <el-button class="filter-item" type="primary" icon="mars-reset" @click="handleResetFilter">重置</el-button>
                -->
                <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="plus" @click="handleCreate()">添加</el-button>
            </el-form>
        </div>

        <el-table :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row style="width: 100%">

            <el-table-column label="ID" width="65" align="center">
                <template slot-scope="scope"> {{scope.row.id}} </template>
            </el-table-column>

            <el-table-column label="车牌号" align="center">
                <template slot-scope="scope"> {{scope.row.carFullNum}} </template>
            </el-table-column>

            <el-table-column label="保养日期" align="center">
                <template slot-scope="scope"> {{scope.row.createTime}} </template>
            </el-table-column>

            <el-table-column label="保养里程数" align="center">
                <template slot-scope="scope"> {{scope.row.carRepairMileage}} </template>
            </el-table-column>

            <el-table-column label="录入人" align="center">
                <template slot-scope="scope"> {{scope.row.operatorName}} </template>
            </el-table-column>

            <el-table-column label="操作" width="250px" align="center">
                <template slot-scope="scope">
                    <el-button size="small" type="primary" @click="handleDetail(scope.row)">详情</el-button>
                    <el-button size="small" type="primary" @click="handleUpdate(scope.row)">编辑</el-button>
                    <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>

        </el-table>

        <div v-show="!listLoading" class="pagination-container">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="listQuery.page"
                    :page-sizes="[10, 20, 30, 50]"
                    :page-size="listQuery.rows"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" @close="handleCloseDialog">
            <el-form :model="form" :rules="rules" ref="form" labelPosition="right" labelWidth="100px">
                <el-form-item label="里程数" prop="carRepairMileage">
                    <el-input v-model="form.carRepairMileage" style="width: 400px"></el-input>
                </el-form-item>
                <el-form-item label="保养内容">
                    <el-input
                            type="textarea"
                            :autosize="{ minRows: 12, maxRows: 4}"
                            placeholder="请输入内容"
                            v-model="form.repairContent">
                    </el-input>
                </el-form-item>


            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button v-if="dialogStatus == 'create'" type="primary" @click="create">确 定</el-button>
                <el-button v-else="" type="primary" @click="update">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="保养详情" :visible.sync="dialogFormVisibleDetail">

            <el-form ref="detail" :model="detail" label-width="120px">
                <el-form-item label="保养里程">
                    {{detail.carRepairMileage}}
                </el-form-item>
            </el-form>

            <el-form ref="detail" :model="detail" label-width="120px">
                <el-form-item label="保养日期">
                    {{detail.createTime}}
                </el-form-item>
            </el-form>

            <el-form ref="detail" :model="detail" label-width="120px">
                <el-form-item label="保养内容">
                    {{detail.repairContent}}
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisibleDetail = false">取 消</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
    import { created, deleted, updated, fetchObject, fetchList } from '@/api/tukuan/car_repair_log'

    export default {
        name: 'app',
        data() {
            return {
                list: null,
                total: null,
                listLoading: true,
                listQuery: {
                    page: 1,
                    rows: 10,
                    carRepairId: this.$route.params.car_repair_id,
                    orderBy: 'create_time desc'
                },
                dialogFormVisible: false,
                dialogFormVisibleDetail: false,
                dialogStatus: '',
                textMap: {
                    update: '编辑',
                    create: '添加'
                },
                form: {
                    carRepairId: this.$route.params.car_repair_id,
                    carFullNum: this.$route.params.car_num,
                    carRepairMileage: null,
                    repairContent: null
                },
                rules: {
                    carRepairMileage: [
                        { required: true, message: '请输入里程数'},
                        { message: '请输入数字', pattern: /^[0-9]+.?[0-9]*$/ }
                    ],
                },
                detail: {
                    carRepairId: this.$route.params.car_repair_id,
                    carFullNum: this.$route.params.car_num,
                    carRepairMileage: null,
                    repairContent: null,
                    createTime: null
                }

            }
        },
        components: {
        },
        created() {
            this.getList()
        },
        filters: {
        },
        methods: {
            getList() {
                this.listLoading = true
                fetchList(this.listQuery).then(response => {
                    this.list = response.data.list
                    this.total = response.data.total
                    this.listLoading = false
                }).catch((error)=>{
                    console.log(error)
                    this.listLoading = false
                })
            },
            handleSizeChange(val) {
                this.listQuery.rows = val
                this.getList()
            },
            handleCurrentChange(val) {
                this.listQuery.page = val
                this.getList()
            },
            handleCloseDialog() {
                this.resetForm()
            },
            handleFilter() {
                this.listQuery.page = 1
                this.getList()
            },
            handleResetFilter() {
                this.$refs.filterForm.resetFields()
                this.getList()
            },
            handleCreate() {
                this.dialogStatus = 'create'
                this.dialogFormVisible = true
            },
            handleUpdate(row) {
                this.dialogStatus = 'update'

                fetchObject(row.id).then(response => {
                    this.dialogFormVisible = true
                    this.form = Object.assign({}, response.data)
                })
            },
            handleDetail(row) {
                fetchObject(row.id).then(response => {
                    this.dialogFormVisibleDetail = true
                    this.detail = Object.assign({}, response.data)
                })
            },
            handleDelete(row) {
                this.$confirm('确定要删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleted(row.id).then(response => {
                        if(response.data.status) {
                            this.$notify({
                                title: '成功', message: response.data.message,
                                type: 'success', duration: 2000
                            })
                            this.getList()
                        } else {
                            this.$notify({
                                title: '发生错误', message: response.data.message,
                                type: 'error', duration: 2000
                            })
                        }
                    })
                }).catch(() => {
                    this.$message({ type: 'info', message: '已取消删除' })
                })
            },
            create() {
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        created(this.form).then(response => {
                            if (response.data.status) {
                                this.$notify({
                                    title: '成功', message: response.data.message,
                                    type: 'success', duration: 2000
                                })
                                this.getList()
                            } else {
                                this.$notify({
                                    title: '操作失败', message: response.data.message,
                                    type: 'error', duration: 2000
                                })
                            }
                            this.dialogFormVisible = false
                        })
                    }
                })

            },
            update() {
                this.$refs['form'].validate((valid) => {
                    if(valid) {
                        updated(this.form).then(response => {
                            if(response.data.status) {
                                this.$notify({
                                    title: '成功', message: response.data.message,
                                    type: 'success', duration: 2000
                                })
                                this.getList()
                            } else {
                                this.$notify({
                                    title: '发生错误', message: response.data.message,
                                    type: 'error', duration: 2000
                                })
                            }
                            this.dialogFormVisible = false
                        })
                    }
                })
            },
            resetForm() {
                this.$refs['form'].resetFields()
                this.files = []
                this.form = {
                    carRepairId: this.$route.params.car_repair_id,
                    carFullNum: this.$route.params.car_num,
                    carRepairMileage: '',
                    repairContent: ''
                }
            }
        }
    }
</script>

<!-- 局部样式 -->
<style rel="stylesheet/scss" lang="scss" scoped>

</style>